/** 
*新增组
 */
<template>
<vp-dialog ref="editDialogRef" :span="1">
    <template #title>
        <vp-label v-if="type == 'add'" zh="新增" en="Add" align="left"/>
        <vp-label v-else zh="修改" en="Edit" align="left"/>
    </template>
    <el-form label-suffix=":" v-loading="loading"  :model="formData" :rules="formRules" ref="formRef" label-width="120px">
        <el-row>
            <vp-col :span="1">
                <el-form-item :label="$t('item.code')" prop="code">
                    <template #label><vp-label zh="代码" en="Code" align="left"/></template>
                    <el-input v-model="formData.code" v-trim :disabled="type==='edit'" maxlength="50"></el-input>
                </el-form-item>
            </vp-col>
            <vp-col :span="1">
                <el-form-item :label="$t('item.name')" prop="name">
                    <template #label><vp-label zh="名称" en="Name" align="left"/></template>
                    <el-input v-model.trim="formData.name" v-trim maxlength="100"></el-input>
                </el-form-item>
            </vp-col>
            <vp-col :span="2">
                <el-form-item :label="$t('workflowcandidate.bmbs')" prop="bmbs">
                    <template #label><vp-label zh="匹配部门" en="Dept. Matching" align="left"/></template>
                    <el-switch
                        v-model="formData.bmbs"
                        :active-value=1
                        :inactive-value=0
                    >
                    </el-switch>
                </el-form-item>
            </vp-col>
        </el-row>
    </el-form>
    <template #footer>
        <vp-btn-confirm :loading="saveLoading" @click="handleSave"/>
    </template>
</vp-dialog>
</template>
<script setup lang="ts" name="editCondidateGroup">
import { ref } from 'vue';
import request from '/@/utils/request';
import validateUtils from '/@/utils/validateUtils';

    const emits = defineEmits(['formSubmited']);
    const formData = ref({
        id:"",
        code:"",
        name:"",
        bmbs:0,
        jxbs:0,
        fjbs:0,
        jdbs:0,
    });
    const formRules = {
        code:[ 
            validateUtils.notNull,
            validateUtils.notChinese
        ],
        name: [
            validateUtils.notNull
        ],
    }

    const loading = ref(false);
    const type = ref('add');
    const saveLoading = ref(false);
    const isValid = ref(false);

    const editDialogRef = ref();
    const formRef = ref();
    const initFormData = ()=>{
        formData.value = {
            id:"",
            code:"",
            name:"",
            bmbs:0,
            jxbs:0,
            fjbs:0,
            jdbs:0,
        }
        isValid.value = false;
    }
    const handleSave = () => {
        if(loading.value){
            return;
        }
        saveLoading.value = true;
        formRef.value.validate((valid: Boolean)=>{
            if(valid){
                request.post("/workflow/candidate/group/"+type.value, {
                    ...formData.value
                }).then(()=>{
                    emits("formSubmited");
                    editDialogRef.value.handleClose();
                }).catch(()=>{
                }).finally(()=>{
                    saveLoading.value = false;
                })
            }else{
                saveLoading.value = false;
            }
        })
    }
    const handleOpen = (_type: string, id: string)=>{
        type.value = _type;
        initFormData();
        editDialogRef.value.handleOpen();
        if(type.value === 'edit'){
            loading.value = true;
            request.get(`/workflow/candidate/group/${id}`).then((res)=>{
                formData.value = {
                    ...formData.value,
                    ...res.data
                };
                loading.value = false;
            })
        }
    }

    const changeFjbs = () =>{
        if(formData.value.fjbs == 1){
            isValid.value = true;
            formData.value.jxbs = 1;
        }else{
            isValid.value = false;
        }
    }

    defineExpose({
        handleOpen,
    })

</script>
